/*
 * Copyright (C) 2005, Velocityme Partnership
 * LoginSessionBean.java
 *
 * Created on 22 February 2005, 07:34
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.LoginStatusBean;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.ChangeDeltaTypePK;
import com.velocityme.interfaces.ChangeDeltaTypeUtil;
import com.velocityme.interfaces.LoginLocal;
import com.velocityme.interfaces.LoginStatusLocal;
import com.velocityme.interfaces.LoginStatusPK;
import com.velocityme.interfaces.LoginStatusUtil;
import com.velocityme.interfaces.LoginUtil;
import com.velocityme.interfaces.PermissionLocal;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.interfaces.PermissionUtil;
import com.velocityme.interfaces.SearchSessionUtil;
import com.velocityme.interfaces.StateUtil;
import com.velocityme.interfaces.UserLocal;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.ServerConfiguration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="LoginSession"
 *           jndi-name="ejb/LoginSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class LoginSessionBean implements SessionBean {
    
    private SessionContext m_context;
    
    private LoginStatusLocal m_statusLoggedOn;
    
    /**
     * Filter the login log for the specified user.
     *
     * @param p_key User's security key
     *
     * @ejb.interface-method view-type="local"
     **/
    public void limitLoginCount(UserLocal p_userLocal) {
        int loginCountLimit = ServerConfiguration.getInstance().getUserLoginCountLimit();
        List loginLocals;
        boolean finished = false;
        while(!finished && (loginLocals = new ArrayList(p_userLocal.getLoginsLocal())).size() > loginCountLimit) {
            Collections.sort(loginLocals, new Comparator() {
                public int compare(Object o1, Object o2) {
                    LoginLocal loginLocal1 = (LoginLocal)o1;
                    LoginLocal loginLocal2 = (LoginLocal)o2;
                    return loginLocal1.getLoginTime().compareTo(loginLocal2.getLoginTime());
                }
            });
            // only remove logins if they are not currently logged on!
            Iterator i = loginLocals.iterator();
            finished = true;
            while(i.hasNext()) {
                LoginLocal loginLocal = (LoginLocal)i.next();
                if (!loginLocal.getLoginStatusLocal().equals(m_statusLoggedOn)) {
                    finished = false;
                    try {
                        LoginUtil.getLocalHome().remove(loginLocal.getPrimaryKey());
                    }
                    catch(NamingException e) {
                        return;
                    }
                    catch(RemoveException e) {
                        return;
                    }
                    break;
                }
            }
        }
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     * @ejb:create-method view-type="remote"
     */
    public void ejbCreate() throws CreateException {
        try {
            m_statusLoggedOn = LoginStatusUtil.getLocalHome().findByPrimaryKey(new LoginStatusPK(new Integer(LoginStatusBean.LOGGED_ON)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
